/**
 * Created by admin on 2016/12/13.
 */
'use strict'

let mor = require("../libs/mongo/mongoHelper");
let querystring = require('querystring');
let dbr = require("../libs/mysql/mysqlHelper");
let jv = require("../libs/sys/myHelper").jv;
let http = require('request-promise');

app.use(app.routes.get('/html/fee/detail', async ctx => {
  ctx.render("fee/detail");
}));

app.use(app.routes.get('/html/fee/list', async ctx => {
  ctx.render("fee/list");
}));

app.use(app.routes.post('/fee/list', async ctx => {
  let params = querystring.parse(ctx.req._parsedUrl.query);

  let userId = ctx.needLogin();
  if (!userId) {
    return;
  }
  let skip = ctx.req.json.skip;
  let take = ctx.req.json.take;

  let results = await dbr.find(`select r.id,
r.status,r.payment_date,r.payment_method_name,r.payment_status,r.contact_name contactor, r.mobile,r.detailed_address address,r.month_number,r.total_amount total,r.create_date,
t.name feeType, t.icon feeIcon
from thwy_fee_payment_record r
join thwy_fee_type t on ( r.fee_type_id = t.id)
where r.member_id=:userId and r.del_flag = 0
order by r.create_date desc
limit :skip,:take`,
    {userId: userId, skip: skip, take: take});

  if (!results || !results.length) {
    ctx.body = [];
    return;
  }

  ctx.body = results;
}));


app.use(app.routes.post('/fee/detail', async ctx => {

  let userId = ctx.needLogin();
  if (!userId) {
    return;
  }

  let orderId = ctx.req.json.orderId;

  if( !orderId){
    ctx.msg = "找不到订单Id";
    return ;
  }

  let results = await dbr.findOne(`select r.id,
r.status,r.payment_date,r.payment_method_name,r.payment_status,r.contact_name contactor, r.mobile,r.detailed_address address,r.month_number,r.total_amount total,r.create_date,
t.name feeType, t.icon feeIcon
from thwy_fee_payment_record r
join thwy_fee_type t on ( r.fee_type_id = t.id)
where r.id = :orderId `,
    {orderId: orderId});

  if (!results) {
    return;
  }


  ctx.body = results;
}));
